﻿using Module.CanJuSheJi.Common;
using Prism.Events;
using System.Windows.Controls;

namespace Module.CanJuSheJi.Views
{
    /// <summary>
    /// 可移动点后台代码
    /// </summary>
    public partial class MovePoint : UserControl
    {
        private bool _isQiPanFanZhuan;
        private bool IsQiPanInvers
        {
            get { return _isQiPanFanZhuan; }
            set
            {
                _isQiPanFanZhuan = value;
            }
        }
        private int Col { get; set; }    // 路径点的列坐标
        private int Row { get; set; }    // 路径点的行坐标
        private bool _canMoveTo;
        public bool CanMoveTo // 棋子是否可移动到此处。可移动到此处时，可接受点击事件，否则，对鼠标透明。
        {
            get { return _canMoveTo; }
            set { _canMoveTo= value; this.IsHitTestVisible = value; }
        }

        public string ModuleName { get; set; }
        private readonly IEventAggregator Ea;

        public MovePoint(IEventAggregator ea)
        {
            InitializeComponent();
            this.Ea = ea;
            CanMoveTo = true;
            PointImage.Visibility = System.Windows.Visibility.Collapsed;

           
            Ea.GetEvent<CanJuSheJiMessageEvent>().Subscribe(
                ModuleMessageProcess, 
                ThreadOption.UIThread,
                false,
                filter => filter.ModuleName == this.ModuleName);
        }
        #region 消息处理
       

        /// <summary>
        /// 订阅消息处理
        /// </summary>
        /// <param name="obj">消息体</param>
        private void ModuleMessageProcess(CanJuSheJiMessage obj)
        {
            switch (obj.MessageName)
            {
                case "reStart": // 重新开局
                    PointImage.Visibility = System.Windows.Visibility.Hidden;
                    CanMoveTo = true;
                    break;
                default: break;
            }
        }
        #endregion

        /// <summary>
        /// 棋子移动目的地标记类，
        /// 在棋子可移动到的有效位置，设置标记。
        /// 点击此标记时，所选棋子移动到此标记位置。
        /// </summary>
        /// <param name="x">列位置</param>
        /// <param name="y">行位置</param>
        public void SetInitPosition(int x, int y)
        {
            Col = x;
            Row = y;
            SetValue(Canvas.LeftProperty, GlobalData.GetQiPanGrid_X(x) + 3);
            SetValue(Canvas.TopProperty, GlobalData.GetQiPanGrid_Y(y) + 3);

        }
        /// <summary>
        /// 可移动点选中时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnClick(object sender, System.Windows.RoutedEventArgs e)
        {
            Ea.GetEvent<CanJuSheJiMessageEvent>().Publish(new CanJuSheJiMessage()
            {
                MessageName = "pointGetClicked",
                SenderFrom = "MovePoint.xaml.cs",
                NewCol = Col,
                NewRow = Row,
                Content = $"【{this.GetType().Name}】：移动点被点击",
                ModuleName=this.ModuleName,
            });
        }

        /// <summary>
        /// 设置可移动点位置
        /// </summary>
        /// <param name="x">行</param>
        /// <param name="y">列</param>
        private void SetPosition(int x, int y)
        {

            if (IsQiPanInvers) // 如果棋盘翻转为上红下黑，则进行坐标转换
            {
                x = 8 - x;
                y = 9 - y;
            }
            SetValue(Canvas.LeftProperty, GlobalData.GetQiPanGrid_X(x) + 3);
            SetValue(Canvas.TopProperty, GlobalData.GetQiPanGrid_Y(y) + 3);
        }

        private void OnClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (CanMoveTo)
            {
                Ea.GetEvent<CanJuSheJiMessageEvent>().Publish(new CanJuSheJiMessage()
                {
                    MessageName = "pointGetClicked",
                    SenderFrom = "MovePoint.xaml.cs",
                    NewCol = Col,
                    NewRow = Row,
                    Content = $"【{this.GetType().Name}】：移动点被点击",
                    ModuleName = this.ModuleName,
                });
            }
        }
    }
}
